import React from "react";
import { Table, Input, Button, Space, Badge } from "antd";
import Highlighter from "react-highlight-words";
import { SearchOutlined, BarsOutlined } from "@ant-design/icons";
import "antd/dist/antd.css";
import { OrderInvoiceList } from "../../../services/auth";
import "./index.css";

export default class InvoiceList extends React.Component {
  constructor() {
    super();
    this.state = {
      searchText: "",
      searchedColumn: "",
      orders: [],
    };
  }

  getColumnSearchProps = (dataIndex) => ({
    filterDropdown: ({
      setSelectedKeys,
      selectedKeys,
      confirm,
      clearFilters,
    }) => (
      <div style={{ padding: 8 }}>
        <Input
          ref={(node) => {
            this.searchInput = node;
          }}
          placeholder={`Search ${dataIndex}`}
          value={selectedKeys[0]}
          onChange={(e) =>
            setSelectedKeys(e.target.value ? [e.target.value] : [])
          }
          onPressEnter={() =>
            this.handleSearch(selectedKeys, confirm, dataIndex)
          }
          style={{ marginBottom: 8, display: "block" }}
        />
        <Space>
          <Button
            type="primary"
            onClick={() => this.handleSearch(selectedKeys, confirm, dataIndex)}
            icon={<SearchOutlined />}
            size="small"
            style={{ width: 90 }}
          >
            Search
          </Button>
          <Button
            onClick={() => this.handleReset(clearFilters)}
            size="small"
            style={{ width: 90 }}
          >
            Reset
          </Button>
          <Button
            type="link"
            size="small"
            onClick={() => {
              confirm({ closeDropdown: false });
              this.setState({
                searchText: selectedKeys[0],
                searchedColumn: dataIndex,
              });
            }}
          >
            Filter
          </Button>
        </Space>
      </div>
    ),
    filterIcon: (filtered) => (
      <SearchOutlined style={{ color: filtered ? "#1890ff" : undefined }} />
    ),
    onFilter: (value, record) =>
      record[dataIndex]
        ? record[dataIndex]
            .toString()
            .toLowerCase()
            .includes(value.toLowerCase())
        : "",
    onFilterDropdownVisibleChange: (visible) => {
      if (visible) {
        setTimeout(() => this.searchInput.select(), 100);
      }
    },
    render: (text) =>
      this.state.searchedColumn === dataIndex ? (
        <Highlighter
          highlightStyle={{ backgroundColor: "#ffc069", padding: 0 }}
          searchWords={[this.state.searchText]}
          autoEscape
          textToHighlight={text ? text.toString() : ""}
        />
      ) : (
        text
      ),
  });

  handleSearch = (selectedKeys, confirm, dataIndex) => {
    confirm();
    this.setState({
      searchText: selectedKeys[0],
      searchedColumn: dataIndex,
    });
  };

  handleReset = (clearFilters) => {
    clearFilters();
    this.setState({ searchText: "" });
  };

  statusCheck = (status) => {
    if (status === "已完成") {
      return "success";
    } else if (status === "待收货") {
      return "processing";
    } else {
      return "error";
    }
  };

  componentDidMount() {
    OrderInvoiceList().then(
      (response) => {
        console.log(response.data);
        this.setState({
          orders: response.data,
        });
      },
      (error) => {
        console.log("error");
      }
    );
  }

  render() {
    const { DisplayDetail } = this.props;
    const columns = [
      {
        title: "订单编号",
        dataIndex: "id",
        key: "id",
        align: "center",
        sorter: (a, b) => a.id - b.id,
        ...this.getColumnSearchProps("id"),
      },
      {
        title: "发票编号",
        dataIndex: "invoice_id",
        align: "center",
        className: "invoice-no",
        sorter: (a, b) => a.id - b.id,
        ...this.getColumnSearchProps("invoice_id"),
      },
      {
        title: "公司",
        dataIndex: "company_name",
        key: "company_name",
        align: "center",
        className: "invoice-name",
        ...this.getColumnSearchProps("company_name"),
      },
      {
        title: "状态",
        dataIndex: "status",
        key: "status",
        align: "center",
        filters: [
          { text: "已完成", value: "已完成" },
          { text: "待收货", value: "待收货" },
        ],
        render: (state) => (
          <Badge status={this.statusCheck(state)} text={state} />
        ),
        onFilter: (value, record) => record.status.indexOf(value) === 0,
      },
      {
        title: "",
        key: "action",
        align: "center",
        render: (record) => (
          <Button
            type="link"
            size="small"
            icon={<BarsOutlined />}
            style={{ fontWeight: "bold", color: "black" }}
            onClick={() => {
              DisplayDetail(record);
            }}
          >
            详情
          </Button>
        ),
      },
    ];

    return (
      <div className="finPage">
        <Table
          columns={columns}
          size="small"
          dataSource={this.state.orders}
          style={{ fontWeight: "bolder" }}
        />
      </div>
    );
  }
}
